#!/bin/bash

echo "===================================="
echo "金融监控系统前端生产环境启动"
echo "===================================="
echo ""

echo "检查Node.js环境..."
if ! command -v node &> /dev/null; then
    echo "错误: 未找到Node.js，请先安装Node.js"
    exit 1
fi

node --version
echo ""

echo "检查依赖包..."
if ! npm list --depth=0 | grep -q "vue"; then
    echo "正在安装依赖包..."
    npm install
fi

echo ""
echo "构建生产环境版本..."
npm run build

if [ $? -ne 0 ]; then
    echo "构建失败，退出..."
    exit 1
fi

echo ""
echo "启动生产环境服务器..."
echo "前端地址: http://localhost:4173"
echo ""

# 后台运行预览服务并将日志输出到文件
mkdir -p logs
nohup npm run preview > logs/frontend.log 2>&1 &
echo "生产环境服务已在后台启动，PID: $!"
echo "日志文件位置: logs/frontend.log"